package com.robusta.cityuate.dialogs;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.robusta.cityuate.R;
import com.robusta.cityuate.objects.User;
import com.robusta.cityuate.services.CityuateBroadCastReceiver;
import com.robusta.cityuate.services.ForgetPasswordService;
import com.robusta.cityuate.utils.ConnectionUtils;

public class ForgetPasswordFragment extends DialogFragment implements
		OnEditorActionListener, TextWatcher {

	private EditText emailField;
	private View view;
	private Button resetButton;
	static final String TAG = ForgetPasswordFragment.class.getName();

	private CityuateBroadCastReceiver receiver = new CityuateBroadCastReceiver() {

		@Override
		public void onSuccess() {
			forgetPSuccess();

		}

		@Override
		public void onFailure() {
			forgetPFail();
		}

		@Override
		public void onCancel() {
			Log.e(TAG, "onCancel");
			progressBar.setVisibility(View.INVISIBLE);
			resetButton.setEnabled(true);
			ConnectionUtils.clearConnection(getActivity(), TAG);
			dismiss();

		}
	};
	private ProgressBar progressBar;

	public ForgetPasswordFragment() {

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.forget_password_view, container);
		emailField = (EditText) view.findViewById(R.id.email);
		emailField.addTextChangedListener(this);
		resetButton = (Button) view.findViewById(R.id.btn_reset);
		resetButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				startForgetPasswordService();
			}
		});
		
		getDialog().setTitle(R.string.forget_password);
		getDialog().getWindow().setSoftInputMode(
				LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		emailField.setOnEditorActionListener(this);

		progressBar = (ProgressBar) view.findViewById(R.id.loading);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		
	}

	@Override
	public void onResume() {
		super.onResume();
		getActivity().registerReceiver(receiver, new IntentFilter(TAG));
		if (ConnectionUtils.isConnecting(getActivity(), TAG)) {
			progressBar.setVisibility(View.VISIBLE);
			resetButton.setEnabled(false);
		} else if (ConnectionUtils.wasConnected(getActivity(), TAG))
			if (ConnectionUtils.isResultSuccessful(getActivity(), TAG))
				forgetPSuccess();
			else
				forgetPFail();
	}

	private void forgetPFail() {
		progressBar.setVisibility(View.INVISIBLE);
		resetButton.setEnabled(true);
		Toast.makeText(getActivity(),
				ConnectionUtils.getExtra(getActivity(), TAG), Toast.LENGTH_LONG)
				.show();
		ConnectionUtils.clearConnection(getActivity(), TAG);
	}

	private void forgetPSuccess() {
		this.dismiss();
		Toast.makeText(getActivity(), getString(R.string.forget_password_sent),
				Toast.LENGTH_LONG).show();
		ConnectionUtils.clearConnection(getActivity(), TAG);

	}

	@Override
	public boolean onEditorAction(TextView arg0, int actionId, KeyEvent arg2) {
		if (EditorInfo.IME_ACTION_DONE == actionId) {
			// Toast.makeText(getActivity(), "test", Toast.LENGTH_LONG).show();
			startForgetPasswordService();
		}
		return false;
	}

	/**
	 * Starts Forget Password
	 */
	private void startForgetPasswordService() {
		if (!ConnectionUtils.isNetworkAvailable(getActivity()))
			ConnectionUtils.showNoConnectionDialog(getActivity());
		else {
			progressBar.setVisibility(View.VISIBLE);
			resetButton.setEnabled(false);
			Intent backgroud_regService = new Intent(getActivity(),
					ForgetPasswordService.class);
			backgroud_regService.setAction(TAG);
			backgroud_regService.putExtra(User.USER_EMAIL, emailField.getText()
					.toString());
			getActivity().startService(backgroud_regService);
			Log.e(TAG, "Fired ForgetPassword service");
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		getActivity().unregisterReceiver(receiver);

		if (!ConnectionUtils.isConnecting(getActivity(), TAG))
			ConnectionUtils.clearConnection(getActivity(), TAG);
	}

	@Override
	public void afterTextChanged(Editable s) {

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		if (s.length() > 0)
			resetButton.setEnabled(true);
		else
			resetButton.setEnabled(false);
	}

}
